package com.sky.utils;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.Reader;
import java.util.HashMap;

@Component
public class BaiduMapUtil {
    private static final String BAIDUMAP_DIRECTIONLITE="https://api.map.baidu.com/directionlite/v1/riding";
    private static final String BAIDUMAP_GEOCODING="https://api.map.baidu.com/geocoding/v3/";


    @Value("${sky.baidu.ak}")
    private String ak;


    /**
     * 根据地址获取经纬度
     * @param address
     * @return
     */
    public String geocoding(String address){
        HashMap<String, String> paramMap = new HashMap<>();
        paramMap.put("address",address);
        paramMap.put("output","json");
        paramMap.put("ak",ak);

        String result = HttpClientUtil.doGet(BAIDUMAP_GEOCODING, paramMap);
      JSONObject jsonObject = JSONObject.parseObject(result);
       /*   String resultJson = jsonObject.getString("result");
        JSONObject resultObject = JSONObject.parseObject(resultJson);*/
        JSONObject resultObject = jsonObject.getJSONObject("result").getJSONObject("location");
        String jingdu = resultObject.getString("lng");
        String weidu = resultObject.getString("lat");
        return weidu+","+jingdu;
    }
    /**
     * 获取两个地点的 路线规划
     * @param origin 起点 维度,经度
     * @param destination 终点 维度,经度
     * @return
     */
    public String directionlite(String origin,String destination){
        HashMap<String, String> paramMap = new HashMap<>();
        paramMap.put("origin",origin);
        paramMap.put("destination",destination);
        paramMap.put("riding_type","1");
        paramMap.put("steps_info","0");
        paramMap.put("ak",ak);
        String result = HttpClientUtil.doGet(BAIDUMAP_DIRECTIONLITE, paramMap);
        return result;
    }
}
